package zttc.itat.dao;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import zttc.itat.idao.IFtpAttachmentDao;
import zttc.itat.model.ComAttachment;
import zttc.itat.model.FtpAttachment;
import zttc.itat.model.TableUtil;

/**
 * ftp�ļ����ݲ���ʵ����
 * @author Administrator
 *
 */
@Repository("ftpAttachmentDao")
public class FtpAttachmentDao extends HibernateDaoSupport implements IFtpAttachmentDao {
	
	 
	@Resource
	public void setSuperSessionFactory(SessionFactory sessionFactory) {
		this.setSessionFactory(sessionFactory);
	}
	
		
	@Override
	public int getFtpttachmentsCount(Map<String, Object> searchMap) {
		 // ���ݶ�Ӧ�����¼������ʶ������������
        @SuppressWarnings("unchecked")
        // ���Session
        Session session = this.getSessionFactory().getCurrentSession();
        // 面向对象的查询方式
        Criteria criteria =  session.createCriteria(FtpAttachment.class).setProjection(Projections.rowCount());
        // ����Լ������
        String recordId = (String) searchMap.get("recordId");
        if(recordId != null && !"".equals(recordId)){
            criteria.add(Restrictions.eq("recordId", recordId));
        }
        String remark = (String) searchMap.get("remark");
        if(remark != null && !"".equals(remark)){
            criteria.add(Restrictions.eq("remark", remark));
        }
        String attaGroupId = (String) searchMap.get("attaGroupId");
        if(attaGroupId != null && !"".equals(attaGroupId)){
            criteria.add(Restrictions.eq("attaGroupId", attaGroupId));
        }
        String attaName = (String) searchMap.get("attaName");
        if(attaName != null && !"".equals(attaName)){
            criteria.add(Restrictions.like("attaName", "%" + attaName + "%"));
        }
        criteria.add(Restrictions.eq("attaStatus", "0"));
        
        return new Integer(String.valueOf(criteria.uniqueResult())) ; 
	}
		
		
	/**
     * 查询视频数据列表
     * @param searchMap
     * @return
     */
	@Override
	public List<FtpAttachment> getFtpttachmentsLst(Map<String, Object> searchMap) {
		 // ���ݶ�Ӧ�����¼������ʶ������������
        @SuppressWarnings("unchecked")
        // ���Session
        Session session = this.getSessionFactory().getCurrentSession();
        // 面向对象的查询方式
        Criteria criteria = session.createCriteria(FtpAttachment.class);
        // ����Լ������
        String recordId = (String) searchMap.get("recordId");
        if(recordId != null && !"".equals(recordId)){
            criteria.add(Restrictions.eq("recordId", recordId));
        }
        String remark = (String) searchMap.get("remark");
        if(remark != null && !"".equals(remark)){
            criteria.add(Restrictions.eq("remark", remark));
        }
        String attaGroupId = (String) searchMap.get("attaGroupId");
        if(attaGroupId != null && !"".equals(attaGroupId)){
            criteria.add(Restrictions.eq("attaGroupId", attaGroupId));
        }
        String attaName = (String) searchMap.get("attaName");
        if(attaName != null && !"".equals(attaName)){
            criteria.add(Restrictions.like("attaName", "%" + attaName + "%"));
        }
        criteria.add(Restrictions.eq("attaStatus", "0"));
        return criteria.list();
	}
	

	/**
	 * 根据主键标识获得文件列表
	 */
	@Override
	public List<FtpAttachment> getAttachments(String recordId) {
		 if (recordId == null || "".equals(recordId.trim())) {
	            return null;
	        }
	        // ���ݶ�Ӧ�����¼������ʶ������������
	        @SuppressWarnings("unchecked")
	        
	        List<FtpAttachment> ftpAttachments = getHibernateTemplate().find(
	                "from  FtpAttachment where recordId = ? and attaStatus=1", recordId.trim());
	        if (ftpAttachments == null || ftpAttachments.isEmpty()) {
	            return null;
	        } else {
	            return  ftpAttachments;
	        }
	}

	
	@Override
	public FtpAttachment getFtpAttachment(String attaId) {
		 if (attaId == null || "".equals(attaId.trim())) {
	            return null;
	        }
	        // ����������ȡ
	        @SuppressWarnings("unchecked")
	        List<FtpAttachment> attachments = getHibernateTemplate().find(
	                "from  FtpAttachment where attaId = ? ", attaId.trim());
	        if (attachments == null || attachments.isEmpty()) {
	            return null;
	        } else {
	            return (FtpAttachment) attachments.get(0);
	        }
	}

	@Override
	public FtpAttachment saveOrUpdateFtpAttachment(FtpAttachment ftpAttachment) {
        if (ftpAttachment.getAttaId() == null || "".equals(ftpAttachment.getAttaId())) {
            String attaId = TableUtil.getPK("FT");
            ftpAttachment.setAttaId(attaId);
        }
        // �������²���
        getHibernateTemplate().saveOrUpdate(ftpAttachment);
        getHibernateTemplate().flush();
        return ftpAttachment;
	}

	@Override
	public void deleteFtpAttachment(String attaId) {
		
		if(attaId==null || "".equals(attaId)){
            return;
        }
        StringBuffer sql = new StringBuffer();
        sql.append("DELETE  from t_ftpattachment WHERE attaId =:attaId ");
        SQLQuery query = this.getSession().createSQLQuery(sql.toString());
        query.setParameter("attaId", attaId);
        query.executeUpdate();
		
	}

	@Override
	public List<FtpAttachment> getFtpAttachmentList(String recordId,
			String group) {
		 if (recordId == null || "".equals(recordId.trim())) {
	            return null;
	        }
	        Session session = this.getSessionFactory().getCurrentSession();
	        Criteria criteria = session.createCriteria(FtpAttachment.class);
	        criteria.add(Restrictions.eq("recordId", recordId))
	                .add(Restrictions.eq("attaGroupId", group)).add(Restrictions.eq("attaStatus", "1"));
	        List<FtpAttachment> attaList = criteria.list();
	        return attaList;
	}



	

}
